<template>
  <div>
    <div p-32rpx bg-white>
      <div text-40rpx>提现金额</div>
      <nut-input :custom-style="mobileInputStyle" type="digit" py-20rpx v-model="price">
        <template #left>
          <span text-70rpx>￥</span>
        </template>
        <template #right>
          <div @click="checkAll" text-24rpx flex align-end flex-col justify-end>
            <div text-right text-main-color>全部提现</div>
            <div mt-10rpx>可提现金额<span>{{ unitPrice(balance) }}</span>元</div>
          </div>
        </template>
      </nut-input>
      <submit @click="confirm" />
    </div>
    <div bg-white flex justify-between items-center pr-20rpx py-30rpx text-28rpx mt-30rpx @click="$navigateTo(`/pages/mine/distribution/history?type=0&id=${distributionData.id}&name=${distributionData.memberName}`)">
      <div text-neutral-600 indent-4>收支明细</div>
    	<div flex items-center><nut-icon size="12" name="rect-right" color="#999999"></nut-icon></div>
    </div>
    <div bg-white flex justify-between items-center pr-20rpx py-30rpx text-28rpx @click="$navigateTo('/pages/mine/distribution/history?type=1')">
      <div text-neutral-600 indent-4>提现记录</div>
    	<div flex items-center><nut-icon size="12" name="rect-right" color="#999999"></nut-icon></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { distribution, cash } from "@/api/goods";
import { unitPrice } from '@/utils/filters';
import { money } from '@/utils/RegExp';
import submit from '@/components/sheet-btn/index.vue';
const distributionData = ref<any>({});  // 当前分销商信息
const price = ref<number>(0); // 提现金额
const balance = ref<number>(0); // 可提现金额
const mobileInputStyle = {
  height: "140rpx",
  fontSize: "54rpx",

}
// 点击提交按钮
function confirm() {
  if (money.test(price.value) && price.value != 0) {
    if (Number(price.value)<1) {
      uni.showToast({
        title: '提现金额不能小于1.00元',
        icon: 'none'
      })
    } else {
       cash({ price: price.value }).then((res) => {
        if(res.data.success){
          uni.showToast({ title: '申请成功!', duration: 2000, icon:"none" });
          setTimeout(()=>{
            uni.navigateBack({delta: 1});
          }, 800)
        }
      });
    }
  } else {
    uni.showToast({
      title: '请输入正确的金额',
      icon: 'none'
    })
  }
}
function checkAll() {
  price.value = balance.value
}

async function init() {
  Promise.all([
    distribution()
  ]).then((res) => {
    if (res[0].data.result) {
      distributionData.value = res[0].data.result;
      balance.value = distributionData.value.canRebate;
    }
  })

}

onMounted(() => {
  init()
})
</script>

<style>
page {
  --nut-input-line-height: 1;
}
</style>
